<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <h1>《{{ queryForm.bookName }}》</h1>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-form ref="form" :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model="queryForm.ChapterName" placeholder="标题" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" native-type="submit" type="primary" @click="handleQuery">查询</el-button>
          </el-form-item>
        </el-form>
        <el-button icon="el-icon-plus" type="success" @click="handleAdd">添加</el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table border ref="tableSort" v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" :height="height">
      <el-table-column label="序号" show-overflow-tooltip width="95">
        <template #default="scope">
          {{ (queryForm.pageNum - 1) * queryForm.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="章节名" prop="title" show-overflow-tooltip width="200" />
      <el-table-column label="内容" prop="content" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ getSimpleText(scope.row.content) }}
        </template>
      </el-table-column>
      <el-table-column label="上传时间" prop="createTime" show-overflow-tooltip width="200">
        <template #default="{ row }">
          {{ turntime(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center" show-overflow-tooltip width="200" />
      <el-table-column label="操作" show-overflow-tooltip width="180px">
        <template #default="{ row }">
          <el-button size="mini" type="warning" @click="handleEdit(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <addChapterEdit ref="edit" @fetch-data="fetchData()" />
  </div>
</template>

<script>
  import { thirteenBitTimestamp } from '@/utils/index.js'
  import { SysFindBooksChapter, DelBooksChapter } from '@/api/table'
  // import addChapterEdit from './components/addChapterEdit'

  export default {
    name: 'ComprehensiveTable',
    components: {
      addChapterEdit,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        elementLoadingText: '正在加载...',
        queryForm: {
          id: 0,
          pageNum: 1,
          pageSize: 20,
          ChapterName: '',
        },
        timeOutID: null,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.queryForm.id = this.$route.query.id
      this.queryForm.bookName = this.$route.query.bookName
      console.log(this.queryForm)
      this.fetchData()
    },
    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    mounted() {},
    methods: {
      getSimpleText(html) {
        var re1 = new RegExp('<.+?>', 'g') // 匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
        var msg = html.replace(re1, '') // 执行替换成空字符
        msg = msg.replace(/&nbsp;/g, ' ') // 去除 &nbsp;
        return msg.slice(0, 100)
      },
      turntime(time) {
        return thirteenBitTimestamp(time)
      },
      handleAdd() {
        this.$refs['edit'].showEdit({ bookId: this.queryForm.id })
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await DelBooksChapter({ chapterId: row.id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        let box = Object.assign({}, this.queryForm)
        delete box.memo
        const { data } = await SysFindBooksChapter(box)
        this.list = data.records
        this.total = data.total
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
